package com.example.tedtalksimproved;

import com.example.tedtalksimproved.cache.MyVolley;
import com.example.tedtalksimproved.loader.VideoInfo;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.webkit.WebView.FindListener;
import android.widget.ImageView;
import android.widget.TextView;
import com.android.volley.toolbox.NetworkImageView;
/**
 * A fragment representing a single Video detail screen. This fragment is either
 * contained in a {@link VideoListActivity} in two-pane mode (on tablets) or a
 * {@link VideoDetailActivity} on handsets.
 */
public class VideoDetailFragment extends Fragment {
	/**
	 * The fragment argument representing the item ID that this fragment
	 * represents.
	 */
	public static final String ARG_ITEM_ID = "item_id";
	
	String title = "Couldn't get it";
	String description = "No description";
	/**
	 * Mandatory empty constructor for the fragment manager to instantiate the
	 * fragment (e.g. upon screen orientation changes).
	 */
	public VideoDetailFragment() {
	}

	VideoInfo vi; 
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		if (getArguments().containsKey(ARG_ITEM_ID)) {
			if(VideoListFragment.mEntries.size()>0){
				vi = VideoListFragment.mEntries.get(Integer.parseInt(getArguments().getString(ARG_ITEM_ID)));
				title = vi.getTitle();
				description = vi.getDescription();
			}else{
				title = "Ted Talks";
				description = "Choose a videod from the list on the left";
			}
			
			
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_video_detail,
				container, false);
		TextView title = (TextView) rootView.findViewById(R.id.detail_title);
		TextView description = (TextView) rootView.findViewById(R.id.detail_description);
		title.setText(this.title);
		description.setText(this.description);
		NetworkImageView image = (NetworkImageView) rootView.findViewById(R.id.detail_image);
		if(vi!=null){
			 if (vi.getImage_url() != null) {
			        image.setImageUrl(vi.getImage_url(), MyVolley.getImageLoader());
			    } else {
			        image.setImageResource(R.drawable.no_image);
			    }
		}
	   
	    image.setOnClickListener(startVideo);

		return rootView;
	}
	
	/*
	 * OnClickListener for image
	 */
	private OnClickListener startVideo = new OnClickListener() {

	    @Override
	    public void onClick(final View v) {
	             switch(v.getId()){
	                 case R.id.detail_image:
	                	 startVideoPlayback();
	                 break;
	              }

	    }
	};
	
	private void startVideoPlayback(){
		Intent detailIntent = new Intent(getActivity().getBaseContext(), VideoPlayer.class);
		detailIntent.putExtra("url", vi.getVideoURL());
		startActivity(detailIntent);
	}
}
